package cn.linstudy.product.service.impl;

import cn.linstudy.product.entity.AttrEntity;
import cn.linstudy.product.service.AttrService;
import cn.linstudy.product.vo.AttrGroupWithAttrsVo;
import com.alibaba.nacos.common.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.linstudy.common.utils.PageUtils;
import cn.linstudy.common.utils.Query;

import cn.linstudy.product.dao.AttrGroupDao;
import cn.linstudy.product.entity.AttrGroupEntity;
import cn.linstudy.product.service.AttrGroupService;
import org.springframework.util.CollectionUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        //select * from pms_attr_group where catelog_id=? and (attr_group_id=key or attr_group_name like %key%)
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if(!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }

        // 为0查询所有
        if( catelogId == 0){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }else {
            wrapper.eq("catelog_id",catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }

    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrs(String catalogId) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        // 查询分组信息
        queryWrapper.eq("catelog_id",catalogId);
        List<AttrGroupEntity> list = this.list(queryWrapper);
        if (!CollectionUtils.isEmpty(list)){
            // 查出所有属性
            List<AttrGroupWithAttrsVo> collect = list.stream().map((item) -> {
                AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
                BeanUtils.copyProperties(item,attrGroupWithAttrsVo);
                List<AttrEntity> relationAttr = attrService.getRelationAttr(attrGroupWithAttrsVo.getAttrGroupId());
                attrGroupWithAttrsVo.setAttrs(relationAttr);
                return attrGroupWithAttrsVo;
            }).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(collect)){
                return collect;
            }
        }
        return Collections.emptyList();
    }

}